package com.schoolai.schoolsystem.service.impl;

import com.schoolai.entity.jpa.SysMenu;
import com.schoolai.entity.jpa.SysUser;
import com.schoolai.schoolsystem.jpa.repository.SysUserRepository;
import com.schoolai.schoolsystem.service.IMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * Copyright(C),2019-2025，XX公司
 * FileName:MenuServiceImpl
 * Author:bobby
 * 创建时间：2025/10/29 09:15
 * Description:菜单接口实现类
 * History:
 * <auth>        <time>       <version>       <desc>
 * 作者          修改时间       版本号         描述
 */
@Service
@Slf4j
public class MenuServiceImpl implements IMenuService {
    @Autowired
    SysUserRepository sysUserRepository;
    @Override
    public List<SysMenu> findMenuByUserId(String userId) {
        List<SysMenu> sysMenuList = new ArrayList<>();
        /// 1、取用户对象
        Optional<SysUser> optionalSysUser =  sysUserRepository.findById(userId);
        if(optionalSysUser.isEmpty()){
            return null;
        }
        SysUser sysUser = optionalSysUser.get();
        //System.out.println(sysUser.getUsername());
        /// 2、从用户对象取菜单
        sysUser.getSysRoleList().forEach(sysRole -> {
            sysMenuList.addAll(sysRole.getSysMenuList());
        });
        ///  3.利用菜单取唯一
        Map<String,Object> menuMap = new HashMap<>();
        sysMenuList.forEach(sysMenu -> {
            // 以菜单类别和排序放入map取唯一，
            menuMap.put(sysMenu.getType().toString()
                            +sysMenu.getSort().toString()
                                +sysMenu.getCode(),
                    sysMenu);
        });
        List<SysMenu> sysMenuOnlyList = new ArrayList<>();
        // 排序输出到list
        menuMap.entrySet().stream().sorted(Map.Entry.comparingByKey()).forEach(entry -> {
            sysMenuOnlyList.add((SysMenu) entry.getValue());
        });
        //System.out.println(sysMenuList.size()+"====="+sysMenuOnlyList.size());
        /// 组成树形菜单,取唯一后jpa自动将自身关联的实体组成树形菜单，底层逻辑不是很清楚，但是的确比MPJ简洁
        List<SysMenu> outSysMenuList = new ArrayList<>();
        sysMenuOnlyList.forEach(sysMenu -> {
            if(sysMenu.getParent().getId().equals("0")){
                outSysMenuList.add(sysMenu);
            }
        });
       // System.out.println("outSysMenuList====="+outSysMenuList.size());
        // 默认从0 测试菜单情况，正式系统不需要

        //AtomicInteger number = new AtomicInteger();;
        //showMenu(outSysMenuList,number);
        return outSysMenuList;
    }
    /**
     * @description:  测试生成菜单情况，正式系统不需要
     * @param:
     * @return:
     * @author
     * @date:
     */
    private void showMenu(List<SysMenu> outSysMenuList,AtomicInteger number) {
        outSysMenuList.forEach(sysMenu -> {
            number.incrementAndGet();
            System.out.println("菜单数："+number+"节点编码"+sysMenu.getCode());
            if(!sysMenu.getSysMenuList().isEmpty()){
                this.showMenu(sysMenu.getSysMenuList(),number);
            }
        });

    }
}
